package com.source.hotelbath.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okgo.OkGo;
import com.source.hotelbath.R;
import com.source.hotelbath.activity.LoginActivity;
import com.source.hotelbath.base.BaseFragment;
import com.source.hotelbath.net.CustomerRequestConstants;
import com.source.hotelbath.utils.MyCookieStore;
import com.source.hotelbath.utils.SharedPreferencesUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Cookie;
import okhttp3.HttpUrl;

/**
 * Created by ZhouMeng on 2017/9/23.
 * 这个是我的模块，使用webview可以在这儿用
 */

public class MineFragment extends BaseFragment {

    public static final String TAG = MineFragment.class.getSimpleName();
    private WebView web_about;
    @Override
    protected View createView(LayoutInflater layoutInflater, ViewGroup var2, Bundle var3) {
        View view=layoutInflater.inflate(R.layout.fragment_mine, null);
        findViews(view);
        return view;
    }

    private void findViews(View view) {
        web_about = (WebView) view.findViewById(R.id.web_about);
        web_about.getSettings().setJavaScriptEnabled(true);
        initCookie();



    }

    private void initCookie() {
        com.lzy.okgo.cookie.store.CookieStore cookieStore = OkGo.getInstance().getCookieJar().getCookieStore();
        //Urls.URL_METHOD

        HttpUrl httpUrl = HttpUrl.parse(SharedPreferencesUtils.getStringData(getContext(),"loginUrl",""));
        List<Cookie> cookies = cookieStore.getCookie(httpUrl);
//        MyCookieStore.synCookies(getContext(),CustomerRequestConstants.newUrl+CustomerRequestConstants.MINE,cookies.toString());
        for (int i=0;i<cookies.size();i++){
            MyCookieStore.synCookies(getContext(),CustomerRequestConstants.newUrl+CustomerRequestConstants.MINE,cookies.get(i).name()+"="+cookies.get(i).value());

        }
        web_about.loadUrl(CustomerRequestConstants.newUrl+CustomerRequestConstants.MINE);
        web_about.setWebViewClient(new WebClient());
    }




    private class WebClient extends WebViewClient {
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("url",url);
            view.loadUrl(url);
            if (url.contains("logout")) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                Intent intent = new Intent(getContext(), LoginActivity.class);
                startActivity(intent);

                getActivity().finish();

            }
            return true;
        }

        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(view, errorCode, description, failingUrl);
            //这里进行无网络或错误处理，具体可以根据errorCode的值进行判断，做跟详细的处理。
             Log.d("errorCode",errorCode+"");
        }

    }

}
